﻿<cfscript>
/** 
* PageList Service
* 
* @hint "PageList Service" 
*/ 

component 
	displayname="public.service.util.pageAdvice" 
	output=false 
	accessors=true 
	{
	
	/* PROPERTIES */
	property name="TotleRow" type="numeric" required="false" default="0" hint="总记录数";
	property name="RowPerPage" type="numeric" required="false" default="20" hint="每页显示多少行";
	property name="StartRow" type="numeric" required="false" default="1" hint="第N行开始";
	property name="Page" type="numeric" required="false" default="1" hint="第N页";
	property name="N" type="numeric" required="false" default="7" hint="显示分页的页码个数";
	
	/**
	* @hint "初始化对象"
	* 
	* @TotleRow "总记录数"
	* @RowPerPage "每页显示多少行"
	* @StartRow "第N行开始"
	* @Page "第N页"
	* @N "显示分页的页码个数"
	*/
	public public.service.util.pageAdvice function init( numeric TotleRow=0, numeric RowPerPage=20, numeric StartRow=1, numeric Page=1, numeric N=7 ) output=false {
		
		setTotleRow( arguments.TotleRow );
		setRowPerPage( arguments.RowPerPage );
		setStartRow( arguments.StartRow );
		setPage( arguments.Page );
		setN( arguments.N );
		
		return this;
	}
	
	/**
	* @hint "计算总页数"
	*/
	public numeric function getPageCount() output=false {
		
		var pageCount = 1;
		
		if ( getTotleRow() ) {
			pageCount = Ceiling( getTotleRow() / getRowPerPage() );
		}
		
		return pageCount;
	}
	
	/**
	* @hint "计算数据偏移量"
	*/
	public numeric function getOffset() output=false {
		return getStartRow()-1;
	}
	
	/**
	* @hint "分页列表"
	*/
	public array function getPageList() output=false {
		var minPage = 0;
		var maxPage = 0;
		var startrowBack = 0;
		var NTback = 0;
		var NTnext = 0;
		var x = 0;
		var nextPage = 0;
		var endrow = 0;
		var startrowNext = 0;
		var lastPage = 0;
		
		var pageList = [];
		
		if ( getTotleRow() gt 0 ) {
			
			minPage = getPage() - Int( getN() / 2 );
			maxPage = getPage() + Int( getN() / 2 );
			
			if ( minPage LT 1 ) {
				maxPage = maxPage - (minPage-1);
				minPage = 1;
			}
			
			if ( maxPage GT Ceiling( getTotleRow() / getRowPerPage() ) ) {
				minPage = minPage - (maxPage - Ceiling( getTotleRow() / getRowPerPage() ));
				maxPage = Ceiling( getTotleRow() / getRowPerPage() );
			}
			
			if ( minPage lt 1 ) {
				minPage = 1;
			}
			
			/* FIRST PAGE */
			if ( getPage() neq 1 ) {
				arrayappend(pageList, ["F", 1, 1] );
			}
			
			/* PREVISE PAGE */
			startrowBack = getStartRow() - getRowPerPage();
			if ( startrowBack GT 0 ) {
				if ( getPage() EQ 1 ) {
					NTback = 1;
				}
				else {
					NTback = getPage() - 1;
				}
				arrayappend(pageList, ["P", startrowBack, NTback] );
			}
			
			/* PageList */
			for ( var x=minPage; x lte maxPage; x++ ) {
				nextPage = x * getRowPerPage() - ( getRowPerPage() - 1 );
				if ( getPage() eq x ) {
					arrayappend(pageList, ["C", nextPage, x] );
				}
				else if ( nextpage LTE getTotleRow() ) {
					arrayappend(pageList, ["D", nextPage, x] );
				}
			}
			
			/* NEXT PAGE */
			endrow = Min( getStartRow() + getRowPerPage() - 1, getTotleRow() );
			startrowNext = endrow + 1;
			if ( startRowNext LTE getTotleRow() ) {
				NTnext = getPage() + 1;
				arrayappend(pageList, ["N", startrowNext, NTnext] );
			}
			
			/* LAST PAGE */
			lastStartRow = getTotleRow() - getRowPerPage() + 1;
			if ( getPage() NEQ maxPage ) {
				lastPage = Ceiling( getTotleRow() / getRowPerPage() );
				arrayappend(pageList, ["L", lastStartRow, lastPage] );
			}
			
		}
		
		return pageList;
	}

}
	
</cfscript>